package com.kun.portal.webframe.service;

import com.kun.portal.webframe.dao.FlightInfoDao;
import com.kun.portal.webframe.po.FlightInfoPo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;
import java.util.Queue;

/**
 * Created by Administrator on 2018/5/8.
 */
@Service
public class FlightInfoService   {
    @Autowired
    private FlightInfoDao flightInfoDao;
    @Autowired
    private EntityManager entityManager;

    public List<FlightInfoPo> queryFlightInfo(String fliter){
        Sort sortx = new Sort(new Sort.Order(Sort.Direction.ASC,"flightKind"));
        Specification specification =  new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
                Predicate effective = cb.notEqual(root.get("effective"),false);
                if(StringUtils.isNotBlank(fliter)){
                    Predicate type= cb.like(root.get("flightType"),fliter);
                    Predicate num= cb.like(root.get("flightNum"),fliter);
                    Predicate kind= cb.like(root.get("flightKind"),fliter);
                    return query.where(effective,cb.or(type,num,kind) ).getRestriction();
                }
                return effective;
            }
        };
        return flightInfoDao.findAll(specification,sortx);
    }

    public Page<FlightInfoPo> query(String fliter, Pageable pageable){

            Specification specification =  new Specification() {
                @Override
                public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
                    Predicate effective = cb.notEqual(root.get("effective"),false);
                    if(StringUtils.isNotBlank(fliter)){
                        Predicate type= cb.like(root.get("flightType"),fliter);
                        Predicate num= cb.like(root.get("flightNum"),fliter);
                        Predicate kind= cb.like(root.get("flightKind"),fliter);
                        return query.where(effective,cb.or(type,num,kind) ).getRestriction();
                    }
                    return effective;
                }
            };

            return flightInfoDao.findAll(specification,pageable);


    }

    public FlightInfoPo saveFlightInfo(FlightInfoPo flightInfoPo){
        return flightInfoDao.save(flightInfoPo);
    }

    public void delete(FlightInfoPo flightInfoPo){

        Query query = entityManager.createQuery("select f from FlightPlanePo f where f.flightInfo.flightInfoId  = :flightInfoId ");
        query.setParameter("flightInfoId",flightInfoPo.getFlightInfoId());
       Object object = query.getFirstResult();
       if(object==null){
           flightInfoDao.delete(flightInfoPo);
       }else{
          flightInfoPo.setEffective(false);
          flightInfoDao.save(flightInfoPo);
       }

    }


}
